package co.com.smart.eagle.client.view.impl;

import java.util.ArrayList;
import java.util.List;

import co.com.smart.eagle.client.view.CrearAplicacionesView;
import co.com.smart.eagle.client.view.impl.components.SmartImageCell;
import co.com.smart.eagle.client.view.impl.smartcelltable.SmartCellTable;
import co.com.smart.eagle.client.view.impl.smartsimplepager.SmartSimplePager;
import co.com.smart.eagle.infrastructure.dto.AplicacionesDTO;

import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

public class CrearApplicacionViewImpl extends Composite implements CrearAplicacionesView{
	
	private Presenter presenter;

	@UiField
	protected TextButton btnGrabar;
	@UiField
	protected TextButton btnConsultar;
	@UiField
	protected TextButton btnLimpiar;
	@UiField
	protected TextBox txtApp;
	@UiField(provided = true)
	protected SmartSimplePager paginador;
	@UiField
	protected SmartCellTable<AplicacionesDTO> tblApps;
	private ListDataProvider<AplicacionesDTO> dataProvider = new ListDataProvider<AplicacionesDTO>();
	
	public enum FocusAppPlace {
		APP, BTN_SAVE, BTN_SEARCH, BTN_CLEAN;
	}

	private static CrearApplicacionViewImplUiBinder uiBinder = GWT
			.create(CrearApplicacionViewImplUiBinder.class);

	interface CrearApplicacionViewImplUiBinder extends
			UiBinder<Widget, CrearApplicacionViewImpl> {
	}

	public CrearApplicacionViewImpl() {
		paginador 		= new SmartSimplePager();
		initWidget(uiBinder.createAndBindUi(this));
		initWidgets();
		cleanForm();
	}
	
	public void initWidgets() {

		createTableApps();
		tblApps.setVisible(true);
		btnLimpiar.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				cleanForm();
				presenter.cargarApps();
			}
		});

		btnGrabar.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				if (presenter.validateCreateApp()) {
					presenter.crearApp();
				}
			}
		});

		btnConsultar.addSelectHandler(new SelectHandler() {

			@Override
			public void onSelect(SelectEvent event) {
				presenter.cargarApps();
			}
		});

	}
	
	/**
	 * Crea la tabla de apps
	 */
	public void createTableApps() {
		if (tblApps.getColumnCount() <= 0) {
			
			TextColumn<AplicacionesDTO> appColumn = new TextColumn<AplicacionesDTO>() {
				@Override
				public String getValue(AplicacionesDTO app) {
					return app.getDsAplicacion();
				}

			};
			
			SmartImageCell imageEliminar = new SmartImageCell("Eliminar aplicaci\u00F3n");
			Column<AplicacionesDTO, String> eliminarColumn = new Column<AplicacionesDTO, String>(imageEliminar) {
				
				@Override
				public String getValue(AplicacionesDTO object) {
					return "images/icoDelete.png,true";
				}
			};
			
			eliminarColumn.setFieldUpdater(new FieldUpdater<AplicacionesDTO, String>() {
				
				@Override
				public void update(int index, AplicacionesDTO object, String value) {
					if(Window.confirm("Esta seguro que desea eliminar la aplicaci\u00F3n ?")){
						presenter.deleteApp(object);
					}
				}
			});


			tblApps.setTitle("Lista de aplicaciones");
			tblApps.setAutoHeaderRefreshDisabled(true);
			tblApps.setWidth("100%");
			tblApps.setEmptyTableWidget(new Label("Sin Datos"));
			
			tblApps.addColumn(appColumn,"Nombre aplicaci\u00F3n");
			tblApps.setColumnWidth(appColumn, 200, Style.Unit.PX);
			appColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			
			tblApps.addColumn(eliminarColumn);
			tblApps.setColumnWidth(eliminarColumn, 30, Style.Unit.PX);
			eliminarColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			
			dataProvider.addDataDisplay(tblApps);
			paginador.setDisplay(tblApps);
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setAppName(String value) {
		this.txtApp.setText(value);
	}

	@Override
	public void setFocus(FocusAppPlace focus) {
		switch (focus) {
		case APP:
			this.txtApp.setFocus(true);
			break;
		case BTN_SAVE:
			this.btnGrabar.focus();
			break;
		case BTN_SEARCH:
			this.btnConsultar.focus();
			break;
		case BTN_CLEAN:
			this.btnLimpiar.focus();
			break;
		default:
			this.txtApp.setFocus(true);
			break;
		}
	}

	@Override
	public boolean validatefields() {
		return false;
	}

	@Override
	public void cleanForm() {
		setAppName("");
		setFocus(FocusAppPlace.APP);
		setApps(new ArrayList<AplicacionesDTO>());
	}

	@Override
	public void setApps(List<AplicacionesDTO> apps) {
		dataProvider.getList().clear();
		dataProvider.getList().addAll(apps);
	}

	@Override
	public void setTextBtnGrabar(String text) {
		this.btnGrabar.setText(text);
	}

	@Override
	public String getAppName() {
		return this.txtApp.getText();
	}

}
